package lc;

import java.util.ArrayList;
import java.util.List;
import util.*;
import org.junit.*;
public class Ex94 {
   /**
 * Definition for a binary tree node.

 */
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        TreeNode cur = root;
        while (cur != null) {
            if (cur.left != null) {
                TreeNode pre = findPre(cur);
                if (pre.right == null) {
                    //没有遍历过
                    pre.right = cur;
                    cur = cur.left;
                    continue;
                } else {
                    //已经遍历过
                    pre.right = null;
                }
            }
            res.add(cur.val);
            cur = cur.right;
            
        }
        return res;
    }

    private TreeNode findPre(TreeNode root) {
        TreeNode cur = root.left;
        while (cur.right != null && cur.right != root) {
            cur = cur.right;
        }
        System.out.println(root.val + ":" + cur.val);
        return cur;
    }
} 

@Test
public void test() {
    Solution s = new Solution();
    TreeNode root = TreeUtils.createTree(new Integer[]{1,null,2,3,4,9,5,6,6,1});
    System.out.println(s.inorderTraversal(root));;
}
}
